<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-cn" style="height:100%;">
<head>
   <head th:include="/new/head::blog_head"></head>
   <head th:include="/blog/public/head::head"></head>
</head>
<body>
<div class="blog-masthead" th:include="/blog/public/top::topv2"></div>
    <div class="blog-body">
     <div class="blog-container">
        <form id="addArticle" class="" th:object="${article}" action="/article/add" method="post">
        
            <div class="layui-form-item">
            <div style="width: 85%;float: left">
                <input type="text" class="layui-input" th:field="*{title}"  placeholder="标题"/>
            </div>
                 <div   style="width:15%;float: right;" th:include="/blog/article_type/select :: select"></div>
            </div>
            <span class="control-label" for="blog-title"
                  th:if="${#fields.hasErrors('title')}"
                  th:errors="*{title}">
            </span>
            <div class="layui-form-item">
            <input class="layui-input" th:field="*{summary}" placeholder="摘要" />
            </div>
            <textarea name="content" id="content" rows="20" cols="100">
            </textarea>
            <input th:field="*{id}" type="hidden"/>
            <input th:field="*{type}" type="hidden"/>
            <input th:field="*{articleText}" type="hidden"/>
            <input name="userId" type="hidden" th:value="${session.user.id}"/>
            <input th:field="*{tag}" type="hidden"/>
            <input th:field="*{enjoy}" type="hidden"/>
            <input th:field="*{Hate}" type="hidden"/>
            <input th:field="*{status}" type="hidden"/>
        </form>
     </div>
    </div>
<div th:include="/blog/public/left::left"></div>
<script>
    $(function () {
        CKEDITOR.replace('content');
        var data = $('input[name="articleText"]').val();
        CKEDITOR.instances.content.setData(data);
        $("#TypeSelect").val($('input[name="type"]').val());
        CKEDITOR.instances.content.on("instanceReady", function () {
            this.addCommand("save", {
                    modes: {
                        wysiwyg: 1,
                        source: 1
                    },
                    exec: function () {
                        addArticle();
                    }
                }
            )
        })
    })
    function addArticle() {
        var data = CKEDITOR.instances.content.getData();
        $('input[name="articleText"]').val(data);

        if (data.toString().trim().length = 0) {
            layer.msg("写点什么吧");
            return;
        }
        $('#addArticle').ajaxSubmit(function (data) {
            layer.msg(data);
            if (data.indexOf("成功") != -1) {
                window.location.href = "/article/list?user="+$("input[name=userId]").val();
            }
        })
    }
    function addTypeFiled(thiz) {
        if($(thiz).val()==-1){
            addTypes()
        }else{
            $('input[name="type"]').val($(thiz).val());
        }
       
    }
    function addTypes() {
        $.get("/article/type/add", {}, function (data) {
            layer.open({
                type: 1,
                area: ['500px', '300px'],
                btn: ['确定'], //按钮
                yes: function (index, layero) {
                    $('#TypeSelect').load('/article/type/select')
                    layer.close(index);
                },
                content: data
            })
        })
    }
</script>
</body>
</html>